<template>
  <div>
    <div style="margin-left: 80px">
      <el-row :gutter="10" style="margin-bottom: 30px">
        <el-descriptions class="margin-top" title="个人信息" :column="2" :size="size" border>
          <template slot="extra">
            <el-button type="primary" size="small" @click="updatePass">修改密码</el-button>
          </template>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-user"></i>
              用户名
            </template>
          {{form.username}}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-mobile-phone"></i>
              密码
            </template>
            ******
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-s-custom"></i>
              角色
            </template>
            <el-tag size="small">{{form.role}}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-date"></i>
              创建时间
            </template>
           {{form.addtime}}
          </el-descriptions-item>
        </el-descriptions>
      </el-row>



    </div>
    <el-dialog title="修改密码" :visible.sync="dialogFormVisible" width="35%">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="原密码:" prop="age">
          <el-input type="password" v-model.number="ruleForm.age" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="修改后密码:" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="确认密码:" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>


</template>

<script>
import request from "@/utils/request";

export default {
  name: "Person",
  data(){
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('密码不能为空'));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          if (value != this.form.password) {
            callback(new Error('密码错误！'));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return{
      form:{},
      user:localStorage.getItem("user")?JSON.parse(localStorage.getItem("user")):{},
      dialogFormVisible: false,
      ruleForm: {
        pass: '',
        checkPass: '',
        age: ''
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        age: [
          { validator: checkAge, trigger: 'blur' }
        ]
      }
    };

  },
  created(){
this.request.get("/users/"+this.user.username).then(res=>{
if (res.code=="200"){
  this.form=res.data
}
})

  },
  methods:{
    updatePass(){
      this.dialogFormVisible=true
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.form.password =this.ruleForm.pass
          request.post("users/update",this.form).then(res =>{
            if (res.code=="200"){
              this.$router.push("/login")
              this.$message.success("修改密码成功！请重新登录")
            }else {
              this.$message.error("修改客户失败")
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }


}
</script>

<style>


</style>